package com.edu.juc.introduction;

import java.util.concurrent.TimeUnit;

class MyNumber {
    int number = 0;
    static int count = 0;

    public synchronized void addNum() {
        try {
            TimeUnit.MILLISECONDS.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        number++;
    }

    public  void addCount() {
        count++;
    }
}

public class SynchronizedTest {
    public static void main(String[] args) {
        MyNumber myNumber = new MyNumber();
        new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                myNumber.addNum();
                System.out.println(Thread.currentThread().getName() + " " + myNumber.number);
            }
        }, "t1").start();

        new Thread(() -> {
            for (int i = 0; i < 100; i++) {
                myNumber.addCount();
                System.out.println(Thread.currentThread().getName() + " " + MyNumber.count);
            }
        }, "t2").start();
    }
}
